package com.ruoyi.admin.controller;


import com.ruoyi.admin.domain.Alarm;
import com.ruoyi.admin.domain.Sensor;
import com.ruoyi.admin.mapper.AlarmMapper;
import com.ruoyi.admin.mapper.SensorMapper;
import com.ruoyi.web.controller.domain.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Select;
import org.apache.poi.ss.usermodel.DataFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/alarm")
@Slf4j
public class AlarmController {

    // 定义阈值
    private static final double TEMPERATURE_THRESHOLD = 30.0;
    private static final double HUMIDITY_THRESHOLD = 80.0;
    private static final double SOIL_N_THRESHOLD = 1800.0;
    private static final double SOIL_P_THRESHOLD = 1800.0;
    private static final double SOIL_K_THRESHOLD = 2000.0;


    //前端 每隔半小时查询一次三小时内的报警数据并且更新报警中心
//后端 每隔一个小时监测最新的数据,判断各个字段是否超过阈值,若超过阈值则插入报警中心数据库
    @Autowired
   public AlarmMapper alarmMapper;

    @Autowired
    public SensorMapper sensorMapper;

    @GetMapping("/info")
    public Result<List<Alarm>> getAlarmInfo(){

        log.info("获取预警中心");
        //直接获取最近的
        List<Alarm> alarmInfo = alarmMapper.getAlarmInfo();
        //筛选每50条才获取 并且只要二十条
        List<Alarm> result=new ArrayList<>();
        int count=0,count1=0;
        for (Alarm alarm : alarmInfo) {
            //再根据距离现在的时间设置距离现在的时间的字符串
            if(count%50==0 && count1<20) {
                alarm.setBetweenTime();
                result.add(alarm);
                count1++;
            }

            if(count1>=20)
                break;
            count++;
        }

        return Result.success(result);
    }


//    @PostConstruct
//    public void init(){
//        InsertAlarmInfo();
//    }

    //定时插入报警数据



    // 定时插入报警数据
    @Scheduled(fixedRate = 10 * 60 * 60 * 1000)
    public void InsertAlarmInfo(){
        //先查询
        Sensor sensor=new Sensor();

       // 获取当前时间
        Calendar calendar = Calendar.getInstance();

        // 减去三个小时
        calendar.add(Calendar.HOUR_OF_DAY, -3);

        // 获取三个小时前的时间
        Date dateThreeHoursAgo = calendar.getTime();

        sensor.setStartTime(dateThreeHoursAgo);
        sensor.setEndTime(new Date());

        List<Sensor> sensors = sensorMapper.selectSensorList(sensor);
        //处理sensors然后插入 alarm表


        // 处理sensors然后插入 alarm表

        List<Alarm> alarms = new ArrayList<>();




        for (Sensor s : sensors) {
            if (s.getTemperature() != null && s.getTemperature() > TEMPERATURE_THRESHOLD) {
                Alarm alarm = new Alarm();
                alarm.setSensorId(s.getId());
                alarm.setTitle("温度异常警告");
                alarm.setContent(String.format("传感器 %s 温度过高，当前温度 %.1f°C，超过预警值 %.1f°C", s.getName(), s.getTemperature().doubleValue(), TEMPERATURE_THRESHOLD));
                alarm.setAlarmTime(s.getTimestamp());
                alarms.add(alarm);
            }

            if (s.getHumidity() != null && s.getHumidity() > HUMIDITY_THRESHOLD) {
                Alarm alarm = new Alarm();
                alarm.setSensorId(s.getId());
                alarm.setTitle("湿度异常警告");
                alarm.setContent(String.format("传感器 %s 湿度过高，当前湿度 %.1f%%，超过预警值 %.1f%%", s.getName(), s.getHumidity().doubleValue(), HUMIDITY_THRESHOLD));
                alarm.setAlarmTime(s.getTimestamp());
                alarms.add(alarm);
            }

            if (s.getSoilN() != null && s.getSoilN() > SOIL_N_THRESHOLD) {
                Alarm alarm = new Alarm();
                alarm.setSensorId(s.getId());
                alarm.setTitle("土壤氮含量异常警告");
                alarm.setContent(String.format("传感器 %s 土壤氮含量过高，当前含量 %.1fppm，超过预警值 %.1fppm", s.getName(), s.getSoilN().doubleValue(), SOIL_N_THRESHOLD));
                alarm.setAlarmTime(s.getTimestamp());
                alarms.add(alarm);
            }

            if (s.getSoilP() != null && s.getSoilP() > SOIL_P_THRESHOLD) {
                Alarm alarm = new Alarm();
                alarm.setSensorId(s.getId());
                alarm.setTitle("土壤磷含量异常警告");
                alarm.setContent(String.format("传感器 %s 土壤磷含量过高，当前含量 %.1fppm，超过预警值 %.1fppm", s.getName(), s.getSoilP().doubleValue(), SOIL_P_THRESHOLD));
                alarm.setAlarmTime(s.getTimestamp());
                alarms.add(alarm);
            }

            if (s.getSoilK() != null && s.getSoilK() > SOIL_K_THRESHOLD) {
                Alarm alarm = new Alarm();
                alarm.setSensorId(s.getId());
                alarm.setTitle("土壤钾含量异常警告");
                alarm.setContent(String.format("传感器 %s 土壤钾含量过高，当前含量 %.1fppm，超过预警值 %.1fppm", s.getName(), s.getSoilK().doubleValue(), SOIL_K_THRESHOLD));
                alarm.setAlarmTime(s.getTimestamp());
                alarms.add(alarm);
            }
        }

        if(alarms.size()!=0)
        alarmMapper.insertAlarmsAsync(alarms);



    }
}
